package validators;

import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpSession;

import controllers.LocaleController;

import beans.UserBeanRemote;
/**
 * @author Zsolt
 */
@ManagedBean
public class UserNameValidator2 implements Validator {

	@EJB
	private UserBeanRemote ubr;

	public UserNameValidator2() {
	}
	/**
	 * Description: method that validates userName introduced by Admin
	 * @author Zsolt
	 * @param FacesContext, UIComponent, Object (contains the userName)
	 */
	@Override
	public void validate(FacesContext arg0, UIComponent arg1, Object arg2)
			throws ValidatorException {

		String enteredName = (String) arg2;
		Pattern p = Pattern.compile("^[a-z0-9_-]{3,15}$");
		Matcher m = p.matcher(enteredName);
		boolean matchFound = m.matches();
		
		//internationalization
		HttpSession session = (HttpSession)
		FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		LocaleController language = (LocaleController) session.getAttribute("language");
		String messageBundleName = "errorMessages.errors";
		if (language.getLanguage()=="ro"){
			messageBundleName += "_ro";
		}
		else if (language.getLanguage()=="hu"){
			messageBundleName += "_hu";
		}
		else if (language.getLanguage()=="de"){
			messageBundleName += "_de";
		}
		ResourceBundle messageBundle = ResourceBundle
				.getBundle(messageBundleName);
		//////////
		
		if (enteredName.equals("") || (enteredName == null)) {
			FacesMessage message = new FacesMessage();
			message.setDetail(messageBundle.getString("usernameEmpty"));
			message.setSummary(messageBundle.getString("usernameEmpty"));
			throw new ValidatorException(message);
		} else {
			if (enteredName.length() < 3) {
				FacesMessage message = new FacesMessage();
				message.setDetail(messageBundle.getString("usernameToShort"));
				message.setSummary(messageBundle.getString("usernameToShort"));
				throw new ValidatorException(message);
			} else {
				if (enteredName.length() > 15) {
					FacesMessage message = new FacesMessage();
					message.setDetail(messageBundle.getString("usernameTooLong"));
					message.setSummary(messageBundle.getString("usernameTooLong"));
					throw new ValidatorException(message);
				}

				else {
					if (!matchFound) {
						FacesMessage message = new FacesMessage();
						message.setDetail(messageBundle.getString("usernameContainsUnapprovedCharacters"));
						message.setSummary(messageBundle.getString("usernameContainsUnapprovedCharacters"));
						throw new ValidatorException(message);
					} else {
						if ((!ubr.existsNameInDB(enteredName))) {
							FacesMessage message = new FacesMessage();
							message.setDetail(messageBundle.getString("usernameDoesntExistsInDatabase"));
							message.setSummary(messageBundle.getString("usernameDoesntExistsInDatabase"));
							throw new ValidatorException(message);
						} 
						}
				}
			}
		}
	}
}
